import { Component,Input, OnInit, OnDestroy } from '@angular/core';

import { Util } from '../../../../utils/util';

import { ShopMonthHistorySrvice } from './shop.month.history.service';

@Component({
  selector: 'shop-month-history',
  templateUrl: './shop.month.history.html',
  providers: [ShopMonthHistorySrvice]
})

export class ShopMonthHistoryComponent {

  @Input() date:any;
  shopInfo: any;
  constructor(private service: ShopMonthHistorySrvice, private util: Util) {

    this.shopInfo = this.util.sessionStore().get('shopDetail');

    this.vm.column.fileName = this.shopInfo.shopName + "月数据";
  }

  vm = {
    "loading": true,
    "data": [],
    "column": {
      fileName: '',
      dim: ['date', 'monthSellMoney', 'monthSellAmount', 'productCount', 'uid'],
      dimText: ['月份', '销售额', '销售量', '动销宝贝数', '店铺id']
    },
    "type": 'line-month'
  };

  columns = ['月份', '销售额', '销售量', '动销宝贝数'];

  ngOnDestroy() {
    if (this.offEvent) {
      this.offEvent.unsubscribe();

    }
  }
  // change

  ngOnChanges(changes) {

    console.log(changes);

    if (changes.date.currentValue && changes.date.currentValue.timeType =='month') {

      console.log(this.date);
      this.vm.loading = true;
      this.getHttp(this.shopInfo,this.date.date);
    }


  }

  export() {
    console.log("export");


    let data = this.util.jsonToArrry(this.vm.data, this.vm.column.dim, this.vm.column.dimText);


    this.util.export(data, this.vm.column.fileName);
  }


  monthSellResulet:any;
  offEvent: any;
  getHttp(shopInfo,date) {

    if(this.vm.loading && this.offEvent){
      this.offEvent.unsubscribe();
    }

    let json = {
      uid: shopInfo.uid
    };
    this.monthSellResulet = {
      'monthSellMoney':0,
      'monthSellAmount':0
    };

    Object.assign(json,date);

    var res = {"code":0,"msg":"成功","data":[{"id":"725677994\u00012017-11","date":"2017-11","crawlDate":"2017-11-11 00:00:00","uid":725677994,"sid":67597230,"shopType":"商城","monthSellAmount":105324975,"monthSellMoney":3.40517533846E9,"productCount":35480}]};

    setTimeout(() => {

      // 合计
      this.monthSellResulet = this.util.getSellAllResult(this.monthSellResulet, res.data);

      this.vm.data = res.data;

      this.vm.loading = false;

    }, 100);


  }



}
